# -*- coding: utf-8 -*-
"""
Created on Fri Aug 23 16:52:41 2013

@author: chico
"""

import sys
from PySide import QtCore, QtGui, QtWebKit
from libatomism import *
import inspect
from string import Template
import datetime

class uiNewClassScriptTemplate(QtGui.QWidget):
    
    def __init__(self,scriptwidget, containerwin):
        super(uiNewClassScriptTemplate, self).__init__()
        self.scriptWidget = scriptwidget
        self.container = containerwin
        self.initUI()
        
    def initUI(self):
        
        vbox = QtGui.QVBoxLayout()
        
        hbox0 = QtGui.QHBoxLayout()
        hbox0.addWidget(QtGui.QLabel("Class name:"))
        self.classNameEdit = QtGui.QLineEdit("myclass")
        hbox0.addWidget(self.classNameEdit)
        vbox.addLayout(hbox0)
        
        hbox1 = QtGui.QHBoxLayout()
        hbox1.addWidget(QtGui.QLabel("Parent class:"))
        self.parentclassName = QtGui.QComboBox()
        self.parentclassName.addItem("msTreeMapper")
        self.addClassAvailable(msTreeMapper,self.parentclassName)
        hbox1.addWidget(self.parentclassName)
        vbox.addLayout(hbox1)
        self.doc = QtGui.QTextEdit("myclass documentation")
        vbox.addWidget(self.doc)
        self.groupBoxNew = QtGui.QGroupBox("New creator")
        vbox.addWidget(self.groupBoxNew )
        self.vboxNew = QtGui.QVBoxLayout()
        self.vboxNew.addWidget(QtGui.QLabel("Documentation:"))
        self.docNew = QtGui.QTextEdit("method New documentation")
        self.vboxNew.addWidget(self.docNew)
        self.addArgNewBttn = QtGui.QPushButton("add argument")
        self.addArgNewBttn.clicked.connect(self.addArgumentNewLayout)
        self.vboxNew.addWidget(self.addArgNewBttn)
        self.groupBoxNew.setLayout(self.vboxNew)
        
        hbox2 = QtGui.QHBoxLayout()
        button = QtGui.QPushButton("Ok")
        button.clicked.connect(self.ok)
        hbox2.addWidget(button)
        hbox2.addWidget(QtGui.QPushButton("Cancel"))
        vbox.addLayout(hbox2)
        
        self.setLayout(vbox)
        
        self.args=[]
        self.argsType=[]
        self.argsdoc=[]
        
    def addArgumentNewLayout(self):
        
        vbox = QtGui.QVBoxLayout()
        
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(QtGui.QLabel("Argument name:"))
        lineedit = QtGui.QLineEdit("var")
        self.args.append(lineedit)
        lineedit.setMinimumWidth(50)
        hbox.addWidget(lineedit)
        hbox.addWidget(QtGui.QLabel("Argument type:"))
        
        combobox =  QtGui.QComboBox()
        combobox.addItem("msTreeMapper")
        self.addClassAvailable(msTreeMapper,combobox)
        hbox.addWidget( combobox )
        self.argsType.append(combobox)
        vbox.addLayout(hbox)
        
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(QtGui.QLabel("doc:"))
        lineedit = QtGui.QLineEdit("arg documentation")
        hbox.addWidget(lineedit)
        self.argsdoc.append(lineedit)
        vbox.addLayout(hbox)
                
        self.vboxNew.addLayout(vbox)
        
        
    def addClassAvailable(self,classebase,combobox):
        
        if(len(classebase.__subclasses__())==0):
            return
            
        for cls in classebase.__subclasses__():
            name =  str(cls)
            if( len(name.split('\''))>1 ):
                name = name.split('\'')[1]
            if( len(name.split('.'))>1 ):
                name = name.split('.')[1]  
            combobox.addItem(name)
            self.addClassAvailable(cls,combobox)
            
    def ok(self):
        
        f = open('newClassTemplate.py', 'r')
        template = str(f.read())
        
        argsString =""
        argsDoc = ""
        argsComp=""
        i = 0
        for arg in self.args:
            argsString = argsString + arg.text()          
            argsDoc = argsDoc + arg.text() + " : " + self.argsdoc[i].text()
            argsComp = argsComp + arg.text() + "(" +self.argsType[i].currentText()+ ")"
            if (arg!= self.args[-1]):
                argsString = argsString + " , "
                argsDoc = argsDoc + "\n        "
                argsComp = argsComp + " , "
                
        d = dict(classname=self.classNameEdit.text(),parentclass=self.parentclassName.currentText(),classdoc=self.doc.toPlainText(),
                 newdoc=self.docNew.toPlainText(),argnew=argsString,argdescription=argsDoc, argnewcompl = argsComp, date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") )
        
        self.scriptWidget.addTab(datetime.datetime.now().strftime("%H:%M")+"*")   
        #text = QtGui.QTextEdit()
        text = self.scriptWidget.currentScript()
        text.filename=""
        #self.scriptWidget.tabWidget.addTab(text,"untittled*")    
        for line in template.splitlines():
            string = Template(line)
            text.append(string.safe_substitute(d))
        self.close()
        self.container.close()